﻿using BLL.Dal;
using BLL.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SysAdmin_Power_List : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["paramType"] != null && Request["paramType"] != "")
        {
            switch (Request["paramType"])
            {
                case "QueryTreePower": GetTreePower(); break;
                case "AddPower": AddPower(); break;
                case "DelPower": DelPower(); break;
                case "QueryModel": QueryModel(); break;
                case "UpdateModel": UpdateModel(); break;
                default: break;
            }
        }
    }

    /// <summary>
    /// 获取所有的树形Org数据
    /// </summary>
    void GetTreePower()
    {

        MenuPowerDAL powerBus = new MenuPowerDAL();
        DataTable dtPower = powerBus.GetList("").Tables[0];

        if (dtPower.Rows.Count > 0)
        {

            System.Text.StringBuilder sbPower = new System.Text.StringBuilder();
            sbPower.Append("[");
            System.Text.StringBuilder sbPowerRoot = new System.Text.StringBuilder();
            DataRow[] rowsRoot = dtPower.Select("parentcode='Root'");
            //第一级节点
            for (int i = 0; i < rowsRoot.Length; i++)
            {
                sbPowerRoot.Append("{");
                sbPowerRoot.Append("\"key\":" + "\"" + rowsRoot[i]["powerCode"] + "\",");
                sbPowerRoot.Append("\"value\":" + "\"" + rowsRoot[i]["powerName"] + "\",");
                sbPowerRoot.Append("\"powerCode\":" + "\"" + rowsRoot[i]["powerCode"] + "\",");
                sbPowerRoot.Append("\"iconCls\":" + "\"" + rowsRoot[i]["iconName"] + "\",");
                sbPowerRoot.Append("\"indexNum\":" + "\"" + rowsRoot[i]["indexNum"] + "\",");
                sbPowerRoot.Append("\"isLeaf\":" + "\"" + rowsRoot[i]["isLeaf"] + "\",");
                sbPowerRoot.Append("\"url\":" + "\"" + rowsRoot[i]["menuCode"] + "\",");


                System.Text.StringBuilder sbPowerChild = new System.Text.StringBuilder();
                DataRow[] rowsChild = dtPower.Select("parentcode='" + rowsRoot[i]["powerCode"] + "'");
                //第二级节点
                for (int j = 0; j < rowsChild.Length; j++)
                {
                    sbPowerChild.Append("{");
                    sbPowerChild.Append("\"key\":" + "\"" + rowsChild[j]["powerCode"] + "\",");
                    sbPowerChild.Append("\"value\":" + "\"" + rowsChild[j]["powerName"] + "\",");
                    sbPowerChild.Append("\"powerCode\":" + "\"" + rowsChild[j]["powerCode"] + "\",");
                    sbPowerChild.Append("\"iconCls\":" + "\"" + rowsChild[j]["iconName"] + "\",");
                    sbPowerChild.Append("\"indexNum\":" + "\"" + rowsChild[j]["indexNum"] + "\",");
                    sbPowerChild.Append("\"isLeaf\":" + "\"" + rowsChild[j]["isLeaf"] + "\",");
                    sbPowerChild.Append("\"url\":" + "\"" + rowsChild[j]["menuCode"] + "\"");

                    //第三级节点
                    DataRow[] rowsLeaf = dtPower.Select("parentcode='" + rowsChild[j]["powerCode"] + "'");
                    System.Text.StringBuilder sbPowerLeaf = new System.Text.StringBuilder();
                    if (rowsLeaf.Length > 0)
                    {
                        for (int k = 0; k < rowsLeaf.Length; k++)
                        {
                            sbPowerLeaf.Append("{");
                            sbPowerLeaf.Append("\"key\":" + "\"" + rowsLeaf[k]["powerCode"] + "\",");
                            sbPowerLeaf.Append("\"value\":" + "\"" + rowsLeaf[k]["powerName"] + "\",");
                            sbPowerLeaf.Append("\"powerCode\":" + "\"" + rowsLeaf[k]["powerCode"] + "\",");
                            sbPowerLeaf.Append("\"iconCls\":" + "\"" + rowsLeaf[k]["iconName"] + "\",");
                            sbPowerLeaf.Append("\"indexNum\":" + "\"" + rowsLeaf[k]["indexNum"] + "\",");
                            sbPowerLeaf.Append("\"isLeaf\":" + "\"" + rowsLeaf[k]["isLeaf"] + "\",");
                            sbPowerLeaf.Append("\"url\":" + "\"" + rowsLeaf[k]["menuCode"] + "\"");
                            sbPowerLeaf.Append("}");
                            sbPowerLeaf.Append(",");
                        }
                        sbPowerChild.Append(",\"children\":" + "[" + sbPowerLeaf.ToString().TrimEnd(',') + "]");
                        sbPowerChild.Append("}");
                        sbPowerChild.Append(",");
                    }
                    else
                    {
                        sbPowerChild.Append("}");
                        sbPowerChild.Append(",");
                    }

                }

                sbPowerRoot.Append("\"children\":" + "[" + sbPowerChild.ToString().TrimEnd(',') + "]");
                sbPowerRoot.Append("}");
                sbPowerRoot.Append(",");
            }

            sbPower.Append(sbPowerRoot.ToString().TrimEnd(','));
            sbPower.Append("]");
            WebHelper.HttpResponseJson(sbPower.ToString());
        }
        else
        {
            WebHelper.HttpResponseJson("[]");
        }
    }

    class TreeGridItem
    {
        /// <summary>
        /// 上级节点标识
        /// </summary>
        public string pid { get; set; }
        /// <summary>
        /// 上级节点名称
        /// </summary>
        public string pname { get; set; }
        public string attributes { get; set; }
        /// <summary>
        /// 本级节点标识
        /// </summary>
        public string id { get; set; }
        /// <summary>
        /// 本级节点名称
        /// </summary>
        public string text { get; set; }
        /// <summary>
        /// 本级节点url
        /// </summary>
        public string url { get; set; }
        /// <summary>
        /// 图标标识
        /// </summary>
        public string iconCls { get; set; }
        /// <summary>
        /// 菜单级别
        /// </summary>
        public string seq { get; set; }
    }

    /// <summary>
    /// 增加菜单操作
    /// </summary>
    void AddPower()
    {
        MenuPowerDAL menuPowerBus = new MenuPowerDAL();
        MenuPower model = new MenuPower();
        model.powerCode = ReqHelper.Get<string>("power_key");
        model.powerName = ReqHelper.Get<string>("power_name");
        //sysMenu.parentCode = ReqHelper.Get<string>("");
        model.parentCode = ReqHelper.Get<string>("parent_code");
        model.indexNum = 0;
        model.isLeaf = true;
        model.isActive = false;
        if (!string.IsNullOrEmpty(Request["is_active"]) && Request["is_active"] == "on")
        {
            model.isActive = true;
        }
        model.menuCode = ReqHelper.Get<string>("power_add_combotree");
        model.powerLevel = 0;
        model.iconName = ReqHelper.Get<string>("iconCls");

        object obj = new { success = true, msg = "数据操作成功" };
        try
        {
            obj = new { success = menuPowerBus.Add(model), msg = "数据操作成功" };
        }
        catch (Exception error)
        {
            obj = new { success = false, msg = error.Message };
        }
        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        WebHelper.HttpResponseJson(response);
    }


    /// <summary>
    /// 删除菜单
    /// </summary>
    void DelPower()
    {
        string strCode = ReqHelper.Get<string>("powerCode");
        MenuPowerDAL powerDal = new MenuPowerDAL();
        object obj = new { success = powerDal.Delete(strCode) };
        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        WebHelper.HttpResponseJson(response);
    }

    /// <summary>
    /// 查询显示项
    /// </summary>
    void QueryModel()
    {
        //string strCode = ReqHelper.Get<string>("menuCode");
        //SysMenuDAL sysMenuDal = new SysMenuDAL();
        //SysMenu sysMenu = sysMenuDal.GetModel(strCode);

        //object obj = new { success = true, message = sysMenu };
        //string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        //WebHelper.HttpResponseJson(response);
    }

    /// <summary>
    /// 更新权限
    /// </summary>
    void UpdateModel()
    {
        MenuPowerDAL PowerBus = new MenuPowerDAL();
        MenuPower model = PowerBus.GetModel(ReqHelper.Get<string>("power_key"));

        model.powerName = ReqHelper.Get<string>("power_name");
        //model.parentCode = ReqHelper.Get<string>("parent_code");
        model.indexNum = 0;
        model.isLeaf = true;
        model.isActive = false;
        if (!string.IsNullOrEmpty(Request["is_active"]) && Request["is_active"] == "on")
        {
            model.isActive = true;
        }
        model.menuCode = ReqHelper.Get<string>("power_edit_combotree");
        model.iconName = ReqHelper.Get<string>("iconCls");

        object obj = new { success = true, msg = "数据操作成功" };
        try
        {
            obj = new { success = PowerBus.Update(model), msg = "数据操作成功" };
        }
        catch (Exception error)
        {
            obj = new { success = false, msg = error.Message };
        }
        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        WebHelper.HttpResponseJson(response);

    }
}